package wangjg.xuexiban.activity.test;

import wangjg.xuexiban.R;
import wangjg.xuexiban.activity.card.BasicCardActivity;
import wangjg.xuexiban.data.CardDAO;
import wangjg.xuexiban.util.Constant;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;

public class CardGalleryActivity extends BasicCardActivity {
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card_list);
		
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setOnItemSelectedListener(new ItemSelectedListener());
		// 定义 Gallery 控件
		gallery.setAdapter(new BaseAdapter(){
			 public int getCount() {
				return getCardDAO().getCount();
			}

			public Object getItem(int position) {
				return position;
			}

			public long getItemId(int position) {
				return position;
			}
			
			public View getView(int position, View convertView, ViewGroup parent) {
				Log.d("getView","begin..");
				if(convertView!=null){
					return convertView;
				}
				return createPageView(position);
			}
		 });
	}
    
	private CardDAO cardDAO;
	private CardDAO getCardDAO(){
		if(cardDAO==null){
			cardDAO = new CardDAO();
			
			SharedPreferences setting = this.getSharedPreferences(Constant.SHARED_PREFERENCES_NAME, 0);
			String root = setting.getString(Constant.URL_CARD_DOWN, Constant.DEFAULT_DATA_ROOT);
			String path = getIntent().getStringExtra("category_path");
			
			cardDAO.setCategoryPath(root+path);
		}
		return cardDAO;
	}
	
}
